package com.nowcoder.code.lcof;

import com.algorithm.model.TreeNode;
import com.algorithm.model.TreeNodeService;

public class JZ79 {

    public static void main(String[] args) {
        TreeNode treeNode = TreeNodeService.getTreeByArray(new int[]{1, 2, 3, 4, 5, 6, 7});
        System.out.println(new JZ79().IsBalanced_Solution(treeNode));
    }

    int max = 0;
    int min = Integer.MAX_VALUE;

    public boolean IsBalanced_Solution(TreeNode pRoot) {
        if (pRoot == null) {
            return true;
        }
        return dfs(pRoot) != -1;
    }

    public int dfs(TreeNode treeNode) {
        if (treeNode == null) {
            return 0;
        }
        int left = dfs(treeNode.left);
        int right = dfs(treeNode.right);
        if (left == -1 || right == -1) {
            return -1;
        }
        if (Math.abs(left - right) > 1) {
            return -1;
        }
        return Math.max(left, right) + 1;
    }


}
